<!--
 * @name: 
 * @Author: 刘大可
 * @Date: 2022-12-28 12:46:04
 * @LastEditors: 刘大可
 * @LastEditTime: 2023-02-16 15:49:27
-->
<template>
    <view class="school-coupon cu-modal bottom-modal" :class="modalName?'show':''" @tap="hideModal">
        <view class="cu-dialog" @tap.stop="" >
            <view class="cu-bar bg-white tit">
                <view class="action text-blue" @tap="hideModal"></view>
                店铺优惠券
                <!-- 金额：{{ price }} -->
                <view class="action" @tap="hideModal">
                    <view class="cuIcon-close"></view>
                </view>
            </view>
            <view class="padding">
               <block v-for="(item,index) in couponList" :key="index">
                    <view class="coupon row">
                        <view class="price row">
                            <view class="key">￥</view>
                            <!-- {{ item.cut_num }} -->
                            {{ item.price }}
                        </view>
                        <view class="flex-col">
                            <view class="name">{{ item.c_name }}</view>
                            <view class="tips">全场商品通用，不可叠加</view>
                        </view>
                        <!-- v-if="item.is_have==0" -->
                        <!-- {{ item.u_cont }}||{{ item.second }} -->
                        <view class="btns row" style="background: #2196F3;" v-if="(item.is_many == 0 && item.u_cont == 0) || (item.is_many == 1 &&item.u_cont< item.second)" @click="ling(item.o_id)" >立即领取</view>
                        <!-- second -->
                        <view class="btns row" style="background: gray;" v-else>已领取</view>
                    </view>
               </block>
               <block v-for="(item, index) in dataList" :key="index">
                    <view class="coupon row">
                        <view class="price row">
                            <view class="key">￥</view>
                            <!-- {{ item.cut_num }} -->
                            {{ item.price }}
                        </view>
                        <view class="flex-col">
                            <view class="name">{{ item.c_name }}</view>
                            <view class="tips">全场商品通用，不可叠加</view>
                        </view>
                        <!-- v-if="item.is_have==0" -->
                        <!-- {{ item.u_cont }}||{{ item.second }} -->
                        <view class="btns row" v-if="price>0" @click="selectCoupon(item)"  >立即使用</view>

                    </view>
                </block>



            </view>

            
        </view>
    </view>
    
</template>
<script>
var app = getApp();

export default {
    data() {
        return {
            modalName:false,
            couponList:[],
            dataList:[]
        }
    },
    created(options) {
        console.log(app);
        
    },
     props: {
        bid: {
            type: String,
        },
        price:{
            type:Number,
            default:0
        }
        // sys: {
        //     type: object,
        // }
    },
    methods: {
        async getCouponList(){
            // let res = await this.request("UserCoupon/storeCoupon", {
            let res = await this.request("UserCoupon/shopcoupon", {
                s_id: uni.getStorageSync('schoolId'),
                store_id: this.bid
            })
            if (res.data.status==200) {
                console.log("🐷🐷");
                console.log(res.data);
                if (res.data.data) {
                    this.couponList = res.data.data
                    this.$root.coupon_list = res.data.data
                    this.$root.storeLingCouponNums = res.data.data.length 
                }else{
                    this.couponList = []
                    this.$root.coupon_list = []
                    this.$root.storeLingCouponNums =0
                }
            }
            console.log(res);
        },
        async ling(o_id){
            // return false
            
            let res = await this.request("UserCoupon/add", {
                s_id: uni.getStorageSync('schoolId'),
                store_id: this.bid,
                o_id
            })
            if (res.data.status == 200) {
               await this.tips.success("领取成功！")
                this.getCouponList()
                this.getList()
            }
        },
        hideModal(){
            this.modalName = false
            try {
                this.$root.calculate()
            } catch (error) {
                
            }
        },
        showModal(){
            this.modalName = true
            this.getCouponList()
            if (this.price>0) {
                // this.getList()
            }
                this.getList()


        },

        getList() {
            var self = this;

            let data = {
                limit: 100,
                page: 1,
                s_id: uni.getStorageSync('schoolId'),
                store_id: this.bid
            }
            app.globalData.util.request({
                url: 'UserCoupon/userStoreCoupon',
                data,
                success(res) {
                    console.log(res)
                    if (res.data.data.length < 4) {
                        self.is_loadmore = false;
                    }
                    self.dataList = [ ...res.data.data]
                    console.log("self.list", self.dataList)
                }
            }, true);
        },
        selectCoupon(item) {
            console.log(item );
            console.log(this.price * 1);
        console.log(    item.price * 1);
            console.log("this.price<item.price=" + this.price * 1 < item.price * 1);

            if (item.type == 1 && item.cut_num * 1 > this.price * 1) {
                this.tips.tips("该优惠券不可用")
                return
            }
            if (this.price * 1 < item.price * 1) {
                this.tips.tips("该优惠券不可用")
                return
            }
            this.$root.storeCoupon = item;
            var totals = this.$root.form.total
            // if (this.$root.userInfo.is_vip == 1) {
            //     totals = (this.$root.form.total - this.$root.form.MinRunPrice).toFixed(2);
            // }
            console.log(this.$root.form.total);
            console.log(totals);
            if (this.$root.y_money>totals) {
                this.$root.y_money= totals
                this.$root.form.y_money= totals
            }
            // return 
            this.$root.calculate();
            this.modalName = false

            // uni.$emit("selectCounper", item)
            return


            // this.hideModal()
            // uni.navigateBack({ delta: 1 })
        },





    },
}
</script>
<style lang="scss">
@import "./main.scss";
    .cu-dialog{
        height:70vh;
        overflow-y: auto;
        .tit{
            position: sticky;
            top: 0px;
        }
    }
</style>